package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam2;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam02Fm extends Fragment implements View.OnClickListener {

    //速断保护时间上限;
    //停电时间
    //复归时间
    //遥测主动上传时间间隔
    //有电复归使能
    //有电复归时间
    //遥测死区主动上传使能
    //遥测死区定值
    //温度采集使能
    //温度异常告警变化值

    private static TextView fastBreakProtoclMaxTime, hasElecTime,stopElecTime,resetTime,mockSendupIntervalTime,
            hasElecResetAble, hasElecResetTime,mockDeadSendupAble,mockDeadFixValue,temperGatherAble,
            temperChangWarnValue;

    private Button param2ReadBtn;

    private Handler handler;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param02,container,false);
        initView(view);
        initEvent();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        return view;
    }

    private void initEvent() {
        fastBreakProtoclMaxTime.setOnClickListener(this);
        hasElecTime.setOnClickListener(this);
        stopElecTime.setOnClickListener(this);
        resetTime.setOnClickListener(this);
        mockSendupIntervalTime.setOnClickListener(this);
        hasElecResetAble.setOnClickListener(this);
        hasElecResetTime.setOnClickListener(this);
        mockDeadSendupAble.setOnClickListener(this);
        mockDeadFixValue.setOnClickListener(this);
        temperGatherAble.setOnClickListener(this);
        temperChangWarnValue.setOnClickListener(this);

        param2ReadBtn.setOnClickListener(btnClickListenrer);
    }

    private void initView(View view) {
        fastBreakProtoclMaxTime = (TextView) view.findViewById(R.id.fastBreakProtoclMaxTime);
        hasElecTime = (TextView) view.findViewById(R.id.hasElecTime);
        stopElecTime = (TextView) view.findViewById(R.id.stopElecTime);
        resetTime = (TextView) view.findViewById(R.id.resetTime);
        mockSendupIntervalTime = (TextView) view.findViewById(R.id.mockSendupIntervalTime);
        hasElecResetAble = (TextView) view.findViewById(R.id.hasElecResetAble);
        hasElecResetTime = (TextView) view.findViewById(R.id.hasElecResetTime);
        mockDeadSendupAble = (TextView) view.findViewById(R.id.mockDeadSendupAble);
        mockDeadFixValue = (TextView) view.findViewById(R.id.mockDeadFixValue);
        temperGatherAble = (TextView) view.findViewById(R.id.temperGatherAble);
        temperChangWarnValue = (TextView) view.findViewById(R.id.temperChangWarnValue);

        param2ReadBtn = (Button) view.findViewById(R.id.param2ReadBtn);
    }
    public static void freshDeviceParam2Model() {
        handler1.sendMessage(new Message());

    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam2 obj =  DeviceParamInfoCache.getDeviceParamMap2().get(MainCahce.chooseDeviceAddr);
            if(obj != null){

                fastBreakProtoclMaxTime.setText(obj.getFastBreakProtoclMaxTime()+"");
                hasElecTime.setText(obj.getHasElecTime()+"");
                stopElecTime.setText(obj.getStopElecTime()+"");
                resetTime.setText(obj.getResetTime()+"");
                mockSendupIntervalTime.setText(obj.getMockSendupIntervalTime()+"");
                hasElecResetAble.setText(obj.getHasElecResetAble()+"");
                hasElecResetTime.setText(obj.getHasElecResetTime()+"");
                mockDeadSendupAble.setText(obj.getMockDeadSendupAble()+"");
                mockDeadFixValue.setText(obj.getMockDeadFixValue()+"");
                temperGatherAble.setText(obj.getTemperGatherAble()+"");
                temperChangWarnValue.setText(obj.getTemperChangWarnValue()+"");
            }else {
                fastBreakProtoclMaxTime.setText(" ");
                hasElecTime.setText(" ");
                stopElecTime.setText(" ");
                resetTime.setText(" ");
                mockSendupIntervalTime.setText(" ");
                hasElecResetAble.setText(" ");
                hasElecResetTime.setText(" ");
                mockDeadSendupAble.setText(" ");
                mockDeadFixValue.setText( " ");
                temperGatherAble.setText( " ");
                temperChangWarnValue.setText( " ");

            }

        }
    };
    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param2ReadBtn:{
                    if(MinaClientThread.session == null){
                        Toast.makeText(getContext(),"请连接wifi！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 2);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };
    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.fastBreakProtoclMaxTime:{
                index = 1;
                str = fastBreakProtoclMaxTime.getText().toString();
                break;
            }
            case R.id.hasElecTime:{
                index = 2;
                str = hasElecTime.getText().toString();
                break;
            }
            case R.id.stopElecTime:{
                index = 3;
                str = stopElecTime.getText().toString();
                break;
            }
            case R.id.resetTime:{
                index = 4;
                str = resetTime.getText().toString();
                break;
            }case R.id.mockSendupIntervalTime:{
                index = 5;
                str = mockSendupIntervalTime.getText().toString();
                break;
            }case R.id.hasElecResetAble:{
                index = 6;
                str = hasElecResetAble.getText().toString();
                break;
            }case R.id.hasElecResetTime:{
                index = 7;
                str = hasElecResetTime.getText().toString();
                break;
            }case R.id.mockDeadSendupAble:{
                index = 8;
                str = mockDeadSendupAble.getText().toString();
                break;
            }
            case R.id.mockDeadFixValue:{
                index = 9;
                str = mockDeadFixValue.getText().toString();
                break;
            }
            case R.id.temperGatherAble:{
                index = 10;
                str = temperGatherAble.getText().toString();
                break;
            }case R.id.temperChangWarnValue:{
                index = 11;
                str = temperChangWarnValue.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                            IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,2,index,Integer.valueOf(edit.getText().toString()));
                            MinaClientThread.session.write(buffer);
                        }catch (Exception e){
                            Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                        }
                    }
                }) .setNegativeButton("取消", null).show();
    }
}
